using MicroMsg.Common.Utils;
using MicroMsg.UI.Page;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Navigation;
namespace MicroMsg.UI.Util
{
	public class PageManager
	{
		private const string TAG = "PageManager";
		public const string ArgPre = "ARGID";
		private static object argLock = new object();
		private static int argId = 0;
		private static Dictionary<string, object> ArgDic = new Dictionary<string, object>();
		private static PageManager mMgr;
		private PhoneApplicationFrame mRootFrame;
		public List<BasePage> basePageList = new List<BasePage>();
		private static string GenArgID()
		{
			int num = 0;
			lock (PageManager.argLock)
			{
				PageManager.argId++;
				num = PageManager.argId;
			}
			return string.Concat(num);
		}
		public static object GetArgOnce(string string_0)
		{
			if (!PageManager.ArgDic.ContainsKey("ARGID" + string_0))
			{
				return null;
			}
			object result = PageManager.ArgDic.get_Item("ARGID" + string_0);
			PageManager.ArgDic.set_Item("ARGID" + string_0, null);
			PageManager.ArgDic.Remove("ARGID" + string_0);
			return result;
		}
		public static object GetArgOnce(PhoneApplicationPage page)
		{
			object result = null;
			IDictionary<string, string> queryString = page.get_NavigationContext().get_QueryString();
			if (queryString.ContainsKey("ARGID"))
			{
				result = PageManager.GetArgOnce(queryString.get_Item("ARGID"));
			}
			return result;
		}
		public static string PutArg(object object_0)
		{
			string text = PageManager.GenArgID();
			PageManager.ArgDic.set_Item("ARGID" + text, object_0);
			return text;
		}
		public static PageManager getInstance()
		{
			if (PageManager.mMgr != null)
			{
				return PageManager.mMgr;
			}
			try
			{
				PhoneApplicationFrame rootFrame = Application.get_Current().get_RootVisual() as PhoneApplicationFrame;
				PageManager.mMgr = new PageManager(rootFrame);
			}
			catch (Exception exception_)
			{
				Log.smethod_2("PageManager", "new PageManager error", exception_);
			}
			return PageManager.mMgr;
		}
		private PageManager(PhoneApplicationFrame RootFrame)
		{
			PageManager <>4__this = this;
			this.mRootFrame = RootFrame;
			Binding binding = new Binding("COMMON_SECONDLEVEL_BK_BRUSH");
			binding.set_Source(R.theme);
			RootFrame.SetBinding(Control.BackgroundProperty, binding);
			Frame arg_6F_0 = RootFrame;
			NavigatedEventHandler navigatedEventHandler = delegate(object sender, NavigationEventArgs e)
			{
				RootFrame.get_Dispatcher().BeginInvoke(delegate
				{
					<>4__this.FrameNavigated();
				});
			};
			arg_6F_0.add_Navigated(navigatedEventHandler);
		}
		private void FrameNavigated()
		{
		}
		public void PopPage()
		{
			if (Enumerable.Count<JournalEntry>(this.mRootFrame.get_BackStack()) > 0)
			{
				try
				{
					this.mRootFrame.RemoveBackEntry();
				}
				catch (Exception)
				{
				}
				if (this.basePageList.get_Count() >= 2)
				{
					BasePage basePage = this.basePageList.get_Item(this.basePageList.get_Count() - 2);
					basePage.DoPageDestory();
					Log.smethod_5("PageManager", "PopPage ,and do DoPageDestory:" + basePage.GetType().get_Name());
					this.basePageList.RemoveAt(this.basePageList.get_Count() - 2);
					return;
				}
				Log.smethod_1("PageManager", "PopPage error,basePageList.Count:" + this.basePageList.get_Count());
			}
		}
		public BasePage GetTopPage()
		{
			if (this.basePageList.get_Count() <= 0)
			{
				return null;
			}
			return this.basePageList.get_Item(this.basePageList.get_Count() - 1);
		}
		public BasePage GetPrevPage()
		{
			if (this.basePageList.get_Count() <= 1)
			{
				return null;
			}
			return this.basePageList.get_Item(this.basePageList.get_Count() - 2);
		}
		public void PopAllPages(bool IsRemoveBackEntry = true)
		{
			Log.smethod_5("PageManager", "PopAllPages");
			int num = Enumerable.Count<JournalEntry>(this.mRootFrame.get_BackStack());
			if (IsRemoveBackEntry)
			{
				for (int i = 0; i < num; i++)
				{
					this.mRootFrame.RemoveBackEntry();
				}
			}
			for (int j = this.basePageList.get_Count() - 2; j >= 0; j--)
			{
				BasePage basePage = this.basePageList.get_Item(j);
				basePage.DoPageDestory();
				Log.smethod_5("PageManager", "do DoPageDestory:" + basePage.GetType().get_Name());
				this.basePageList.RemoveAt(j);
			}
		}
		public void ClearStackToChatPage()
		{
			if (Enumerable.Count<JournalEntry>(this.mRootFrame.get_BackStack()) <= 0)
			{
				return;
			}
			if (this.basePageList.get_Count() <= 1)
			{
				return;
			}
			if (this.basePageList.get_Item(this.basePageList.get_Count() - 1).GetType().get_Name() == "MainPage")
			{
				return;
			}
			for (int i = this.basePageList.get_Count() - 2; i >= 0; i--)
			{
				BasePage basePage = this.basePageList.get_Item(i);
				if (!(basePage.GetType().get_Name() != "MainPage"))
				{
					break;
				}
				this.mRootFrame.RemoveBackEntry();
				basePage.DoPageDestory();
				Log.smethod_5("PageManager", "do DoPageDestory:" + basePage.GetType().get_Name());
				this.basePageList.RemoveAt(i);
			}
			this.basePageList.get_Item(this.basePageList.get_Count() - 1).GoBack();
		}
		public int GetPageCount()
		{
			return Enumerable.Count<JournalEntry>(this.mRootFrame.get_BackStack());
		}
		public void Add(BasePage basePage)
		{
			if (basePage == null)
			{
				return;
			}
			this.basePageList.Add(basePage);
			Log.smethod_5("PageManager", "basePageList.Add:" + basePage.GetType().get_Name());
		}
		public void RemovePage(BasePage basePage)
		{
			if (basePage == null)
			{
				return;
			}
			Log.smethod_5("PageManager", "basePageList.RemovePage:" + basePage.GetType().get_Name());
			if (this.basePageList.get_Count() <= 0)
			{
				Log.smethod_1("PageManager", "RemovePage:basePageList.Count <= 0");
				return;
			}
			BasePage basePage2 = this.basePageList.get_Item(this.basePageList.get_Count() - 1);
			if (basePage2 != basePage)
			{
				Log.smethod_1("PageManager", "lastPage != basePage,lastPage:" + basePage2.GetType().get_Name() + ",base page:" + basePage.GetType().get_Name());
				this.DumpPageStack();
				return;
			}
			this.basePageList.RemoveAt(this.basePageList.get_Count() - 1);
		}
		public void DumpPageStack()
		{
			string text = "";
			using (List<BasePage>.Enumerator enumerator = this.basePageList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					BasePage current = enumerator.get_Current();
					text = text + current.GetType().get_Name() + ",";
				}
			}
			Log.smethod_5("PageManager", "dumpPageStack:" + text);
		}
		public static bool IsTalkingToUser(string username)
		{
			return !string.IsNullOrEmpty(username) && ChatContainerPage.getCuChatContainerPage() != null && ChatContainerPage.getCuChatContainerPage().HasChatPageShow() && MainPage.mainPageInstance != null && ChatContainerPage.getCuChatContainerPage().mChatPageCtrl.cuUser == username;
		}
	}
}
